package com.redlight.pool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by chao on 2019/1/23.
 */
public abstract class AbstractRunnablePoolTask<T> implements IPoolRunnableTask<T> {

    private Logger logger= LoggerFactory.getLogger(getClass());
    protected String threadId="";//线程id
    protected  long startTim=0;//开始时间
    protected Thread t=null;
    protected  ThreadPool pool=null;
    protected T param=null;
    protected boolean runFlg=true;

//    @Override
    public Thread getThread() {
        return t;
    }
    public boolean getRunFlg(){
        return runFlg;
    }

    @Override
    public void init(String id, ThreadPool pool, T param) {
        this.param=param;
        this.threadId=id;
        this.pool=pool;
    }
    @Override
    public void  run(){
        startTim=System.currentTimeMillis();
        t=Thread.currentThread();
        try {
            if(param!=null){
                excuteTask(param);
            }
            runFlg=false;
        }catch (Exception e){
            logger.info(threadId+"线程池运行中出现异常",e);
        }
        pool.removeTask(this);
        runFlg=false;
    }
    public abstract void excuteTask(T param)throws  Exception;
    public void stop()  {
        if(t.isAlive()){
            t.interrupt();
        }
    }

    @Override
    public String getThreadId() {
        return threadId;
    }
    public void setThreadId(String threadId){
        this.threadId=threadId;
    }

    @Override
    public long getStartTim() {
        return startTim;
    }

    public void  setStartTim(long startTim){
        this.startTim=startTim;
    }

    public Thread getT() {
        return t;
    }
    public void setT(Thread t){
        this.t=t;
    }

    public ThreadPool getPool(){
        return pool;
    }

    public void setPool(ThreadPool pool){
        this.pool=pool;
    }

    public  T getParam(){
        return param;
    }

    public void set(T param){
        this.param=param;
    }

    public boolean isRunFlg(){
        return runFlg;
    }
    public void setRunFlg(boolean runFlg){
        this.runFlg=runFlg;
    }

}
